import java.io.DataInputStream;
import java.io.IOException;

public class ClientInputListener implements Runnable {
	private DataInputStream in;
	
	private boolean ready;
	private int input;
	
	public ClientInputListener(DataInputStream in) {
		this.in = in;
		input = -1;
	}

	@Override
	public void run() {
		for(;;) {
			if(!ready) {
				try {
					input = in.readInt();
				} catch (IOException e) {
					e.printStackTrace();
					System.exit(1);
					return;
				}
				
				if(input != -1) {
					ready = true;
				}
			}
			
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public boolean isReady() {
		return ready;
	}

	public int getInput() {
		int out = input;
		input = -1;
		ready = false;
		return out;
	}
}